const appid = "wx271cd2bbd7cd7d04"
const appSecret = "3edfcb114dd2d488fb0f640e65fcc96d"

const jwt = require("jsonwebtoken")

function getToken(openid) {
	return jwt.sign({
		openid: openid
	}, appSecret, {
		expiresIn: 60 * 60 * 24
	})
}

function verifyToken(token) {
	return jwt.verify(token, appSecret, (err, data) => {
		if (err) {
			console.log("err", err)
			if (err.name == 'TokenExpiredError') { //token过期
				let str = {
					code: 500,
					message: 'token过期'
				}
				return str;
			} else if (err.name == 'JsonWebTokenError') { //无效的token
				let str = {
					code: 500,
					message: '无效的token'
				}
				return str;
			}
		} else {
			return {
				...data,
				...{
					code: 200
				}
			};
		}
	})
}

module.exports = {
	appid: appid,
	secret: appSecret,
	getToken: getToken,
	verifyToken: verifyToken
}
